<?php  namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model
{
	//操作商品表单
	protected $table = 'goods';
	//自动验证
	protected $validate = array(
		/*array('cate_cid','required','分类不能为空',3,3),
		array('sname','required','名称不能为空',3,3),
		array('spcontent','required','商品详情不能为空',3,3),
		array('service','required','售后服务不能为空',3,3),*/
	);
	//自动方法
	protected $auto = array(
		//上架时间
		array('grounding','time','function',3,1),
		//管理员uid
		array('adname_uid','adnameuid','method',3,1),
	);
	//管理员方法
	public function adnameuid()
	{
//		p($_SESSION['uid']);
		return $_SESSION['uid'];
	}
	/*
	 * 执行添加
	 */
	public function store()
	{
		//自动验证
		if(!$this->create()) return false;
		/*$_POST  = array_filter($_POST);
		$_POST['attr']  = array_filter($_POST['attr']);
		foreach ($_POST['spec'] as $k => $v) {
			$_POST['spec'][$k]['color']=array_filter($v['color']);
			$_POST['spec'][$k]['added']=array_filter($v['added']);
		}
		p($_POST);die;*/
		//获取分类ID
		$typeattrid = $_POST['cate_cid'];
		//查看分类，条件分类ID，获取类型ID
		$typeattrData = Db::table('cate')->where('cid',$typeattrid)->pluck('oftype_lid');
		//类型ID压入data['typeattr_lid']；
		$this->data['typeattr_lid']=$typeattrData;
		//执行商品添加返回主键ID
		$sid = $this -> add();
		//实例化商品属性
		$GoodsattrData = new \Common\Model\Goodsattr;
		//循环添加$_POST['attr']商品属性
		foreach ($_POST['attr'] as $k => $v) {
			$data = array(
				"shopvalue"=>$v,
				"attribute_xid"=>$k,
				"goods_sid"=>$sid,
			);
			$GoodsattrData->add($data);
		}
		//循环添加$_POST['spec']商品规格
		foreach ($_POST['spec'] as $k => $v) {
			foreach ($v['color'] as $key => $value) {
				$data = array(
				"shopvalue"=>$value,
				"addition"=>$v['added'][$key],
				"attribute_xid"=>$k,
				"goods_sid"=>$sid,
			);
			$GoodsattrData->add($data);
			}
		}
		//实例化商品详情
		$DetailsData = new \Common\Model\Details;
		//循环添加$_POST['smb']图册
		foreach ($_POST['smb'] as $k => $v) {
			//data数组处理
			$datas["small"][] = Image::thumb($v,"Upload/goods/".date("ymd")."/small_".mt_rand(1, 100000).".jpg",100,100,5);
			$datas["mean"][] = Image::thumb($v,"Upload/goods/".date("ymd")."/mean_".mt_rand(1, 100000).".jpg",250,250,5);
			$datas["big"][] = $v;
		}
			$datas["small"] = implode(',', $datas["small"]);
			$datas["mean"] = implode(',', $datas["mean"]);
			$datas["big"] = implode(',', $datas["big"]);
			$datas['spcontent']=$_POST['spcontent'];
			$datas['service']=$_POST['service'];
			$datas['details_sid']=$sid;
//			p($_POST['smb']);
//			p($data);die;
			$DetailsData->add($datas);
		/*//实例化货品
		$KindsData = new \Common\Model\Kinds;
		foreach ($_POST['spec'] as $k => $v) {
			foreach ($v['color'] as $key => $value) {
				//添加货号
				$KindsData->data['artno']=date('Ymd',$_POST['artno']).$k.$key;
				//添加库存
				$KindsData->data['stock']=$_POST['stock'].$key;
			}
		}
		//添加库存
		$KindsData->data['stock']=$_POST['stock'];
		//添加商品ID
		$KindsData->data['goods_hid']=$sid;
		//货品的add添加
		$KindsData->add();*/
		return TRUE;
	}
	public function edit($sid)
	{
		if(!$this->create()) return false;
//		p($_POST);die;
		//save编辑，条件商品ID
		$this->where('sid',$sid)->save();
		//实例化商品属性
		$delgoodsattrData = new \Common\Model\Goodsattr;
		//执行商品属性删除，条件商品ID
		$delgoodsattrData -> where('goods_sid',$sid)->delete();
		//商品属性的添加
		foreach ($_POST['attr'] as $k => $v) {
			$data = array(
				"shopvalue"=>$v,
				"attribute_xid"=>$k,
				"goods_sid"=>$sid,
			);
			$delgoodsattrData->add($data);
		}
		foreach ($_POST['spec'] as $k => $v) {
			foreach ($v['color'] as $key => $value) {
				$data = array(
				"shopvalue"=>$value,
				"addition"=>$v['added'][$key],
				"attribute_xid"=>$k,
				"goods_sid"=>$sid,
			);
			$delgoodsattrData->add($data);
			}
		}
		//实例化商品详情
		$DetailsData = new \Common\Model\Details;
		//删除旧数据
		$DetailsData ->where('details_sid',$sid)->delete();
		//循环添加$_POST['smb']图册
		foreach ($_POST['smb'] as $k => $v) {
			//data数组处理
			$datas["small"][] = Image::thumb($v,"Upload/goods/".date("ymd")."/small_".mt_rand(1, 100000).".jpg",100,100,5);
			$datas["mean"][] = Image::thumb($v,"Upload/goods/".date("ymd")."/mean_".mt_rand(1, 100000).".jpg",250,250,5);
			$datas["big"][] = $v;
		}
			$datas["small"] = implode(',', $datas["small"]);
			$datas["mean"] = implode(',', $datas["mean"]);
			$datas["big"] = implode(',', $datas["big"]);
			$datas['spcontent']=$_POST['spcontent'];
			$datas['service']=$_POST['service'];
			$datas['details_sid']=$sid;
//			p($_POST['smb']);
//			p($data);die;
			$DetailsData->add($datas);
		//实例化货品
		$delKindsData = new \Common\Model\Kinds;
		//货品删除，条件商品ID
		$delKindsData -> where('goods_hid',$sid) -> delete();
		return TRUE;
	}
/*
 * 删除
 */
	public function del($sid)
	{
		//实例化商品详情
		$delgoodsattrData = new \Common\Model\Goodsattr;
		//执行删除，条件商品ID
		$delgoodsattrData -> where('goods_sid',$sid)->delete();
		//实例化商品详情
		$delDetailsData = new \Common\Model\Details;
		//执行删除，添加商品ID
		$delDetailsData -> where('details_sid',$sid) ->delete();
		//实例化货品
		$delKindsData = new \Common\Model\Kinds;
		//执行删除，条件商品ID
		$delKindsData -> where('goods_hid',$sid) -> delete();
		//商品删除
		$this->where('sid',$sid)->delete();
		return true;
	}
}
